package com.example.practice;


import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_7 extends Fragment {
    private List<Fragment> fragmentList = new ArrayList<>();
    private View view;
    private ViewPager vpg;
    private View point;
    private LinearLayout linearLayout_points;
    private FragmentPagerAdapter fragmentPagerAdapter;
    public Fragment_7() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_7, container, false);
        initView();
        return view;
    }

    private void initView() {
        vpg=view.findViewById(R.id.vpg);
        linearLayout_points = view.findViewById(R.id.point_layout);//滑动的点
        fragmentList.add(new FragmentDie1());
        fragmentList.add(new FragmentDie2());
        fragmentList.add(new FragmentDie3());
        fragmentList.add(new FragmentDie4());
        fragmentList.add(new FragmentDie5());
        fragmentList.add(new FragmentDie6());
        fragmentPagerAdapter = new FragmentAdpterDie(getChildFragmentManager(), fragmentList);
        vpg.setAdapter(fragmentPagerAdapter);
        final SharedPreferences sp=getActivity().getSharedPreferences("www",getActivity().MODE_PRIVATE);
        String www = sp.getString("www", "");
        vpg.setCurrentItem(Integer.parseInt(www));

        for(int i=0;i<fragmentList.size();i++){
            point=new View(getContext());
            point.setBackgroundResource(R.drawable.point_select);//把“点”给View
            point.setEnabled(false);//默认为false
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(30,30);//点的大小
            layoutParams.leftMargin=10;//每个点的相隔的距离
            linearLayout_points.addView(point,layoutParams); //页面接收“点”的属性
        }
        linearLayout_points.getChildAt(0).setEnabled(true);//给第一个点设置为选中

        vpg.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<fragmentList.size();i++) {
                    if (i == position) {
                        linearLayout_points.getChildAt(i).setEnabled(true);//选中的点一个样式
                    } else {
                        linearLayout_points.getChildAt(i).setEnabled(false);//没选中的一个样式
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
